#include <iostream>
#include <stdexcept>

using namespace std;

template<typename T>

class Stack
{
    private:
    typedef struct node
    {
        T data;
        node *next;
        node (T d):data(d),next(NULL){}
    }node, *pnode;
    pnode head;
    int size;
    public:
    Stack():head(NULL),size(0){};
    ~Stack()
    {
        while (head)
        {
            pnode temp = head;
            head = head->next;
            delete temp;/* code */
        }
    };
    void push(T value);
    T pop();
    T top() const;
    int getsize() const;
};

template<typename T>
void Stack<T>::push(T value)
{
    pnode newnode = new node(value);
    newnode->next = head;
    head = newnode;
    size++;
}
template<typename T>
T Stack<T>::pop()
{
    if (head==NULL){
        throw underflow_error("Stack is empty");
    }
    T result = head->data;
    pnode temp = head;
    head = head->next;
    delete temp;
    size--;
    return result;
}
template<typename T>
T Stack<T>::top() const{
    
    if (head==NULL){
        throw underflow_error("Stack is empty");
    }
    return head->data;
}
template<typename T>
int Stack<T>::getsize() const
{
    return size;
}



